<template>
	<view>
	<view class="page">
		<view class="detail-box-item">
			<view class="title">
				基础信息
			</view>
			<view class="content-box">
				<view class="cell-item">
					<view class="left-box">
						采购员编号
					</view>
					<view class="right-box">
						cfg12d456d456465sfa
					</view>
				</view>
				<view class="cell-item">
					<view class="left-box">
						采购优先级
					</view>
					<view class="right-box">
						1级紧急
					</view>
				</view>
				<view class="cell-item">
					<view class="left-box">
						采购订单状态
					</view>
					<view class="right-box">
						待审核
					</view>
				</view>
				<view class="cell-item">
					<view class="left-box">
						申请时间
					</view>
					<view class="right-box">
						2022-04-11 16:46:53
					</view>
				</view>
			</view>
		</view>
		<view class="detail-box-item">
			<view class="title">
				采购申请人信息
			</view>
			<view class="info-item">
				<view class="left-box">
					<image class="user-icon" src="/static/image/people_icon.png" mode="aspectFit"></image>
					<view class="info-message">
						<view class="name">
							杨雪婷 133333333333
						</view>
						<view class="address">
							施工方哈迪斯规划局卡话感觉卡萨户籍卡
						</view>
					</view>
					
				</view>
				<view class="right-box">
					<image v-if="detailinfo.mobile && detailinfo.mobile !=''" class="phone-icon" src="/static/image/order/phone_icon.png" mode="aspectFit"
						@click="phoneCall(detailinfo.mobile)"></image>
				</view>
			</view>
		</view>
		<view class="detail-box-item">
			<view class="title">
				采购物料
			</view>
			<view class="card-item">
				<view class="product-item" @click="materialOpen">
					<view class="img-box">
						<image class="img" src="/static/image/img_icon.png" mode="aspectFit"></image>
					</view>
					<view class="right-box">
						<view class="product-title">
							{{item.materialShortName}}
						</view>
						<view class="product-id">
							WL533253278529375839257
						</view>
						<view class="size">
							大小：6/重量：12
						</view>
						<view class="sign">
							件
						</view>
					</view>
				</view>
				<view class="table-box">
					<view class="table-row">
						<view class="table-item">
							<view class="item-value">
								<view class="table-title">
									请购数：
								</view>
								<view class="table-value">
									<view class="num">
										10
									</view>
									<view class="sign">
										/件
									</view>
								</view>
							</view>
						</view>
						<view class="table-item">
							<view class="item-value">
								<view class="table-title">
									提购数：
								</view>
								<view class="table-value">
									<view class="num">
										12
									</view>
								</view>
							</view>
						</view>
						<view class="table-item">
							<view class="item-value">
								<view class="table-title">
									提购价格：
								</view>
								<view class="table-value">
									<view class="num">
										22.44
									</view>
								</view>
							</view>
						</view>
					</view>
					<view class="table-row">
						<view class="table-item">
							<view class="item-value">
								<view class="table-title">
									含税单价：
								</view>
								<view class="table-value">
									<view class="num">
										22.44
									</view>
								</view>
							</view>
						</view>
						<view class="table-item">
							<view class="item-value">
								<view class="table-title">
									含税总价：
								</view>
								<view class="table-value">
									<view class="num">
										269.44
									</view>
								</view>
							</view>
						</view>
						<view class="table-item">
							<view class="item-value">
								<view class="table-title">
									税率：
								</view>
								<view class="table-value">
									<view class="num">
										0.02
									</view>
								</view>
							</view>
						</view>
					</view>
					<view class="table-row">
						<view class="table-item">
							<view class="item-value">
								<view class="table-title">
									不含税单价：
								</view>
								<view class="table-value">
									<view class="num">
										22
									</view>
								</view>
							</view>
						</view>
						<view class="table-item">
							<view class="item-value">
								<view class="table-title">
									不含税总价：
								</view>
								<view class="table-value">
									<view class="num">
										269.44
									</view>
								</view>
							</view>
						</view>
					</view>
					<view class="table-row">
						<view class="table-item">
							<view class="item-value">
								<view class="table-title">
									含税单价：
								</view>
								<view class="table-value">
									<view class="num">
										22.44
									</view>
								</view>
							</view>
						</view>
						<view class="table-item">
							<view class="item-value">
								<view class="table-title">
									含税总价：
								</view>
								<view class="table-value">
									<view class="num">
										269.44
									</view>
								</view>
							</view>
						</view>
						<view class="table-item">
							<view class="item-value">
								<view class="table-title">
									税率：
								</view>
								<view class="table-value">
									<view class="num">
										0.02
									</view>
								</view>
							</view>
						</view>
					</view>
					<view class="table-row">
						<view class="table-item">
							<view class="item-value">
								<view class="table-title">
									项目名称：
								</view>
								<view class="table-value">
									<view class="num">
										{{item.projectName ? item.projectName : "&nbsp;"}}
									</view>
								</view>
							</view>
						</view>
						<view class="table-item">
						</view>
						<view class="table-item">
							<view class="item-value">
								<view class="table-title" v-if="item.projectLeader">
									项目负责人：
								</view>
								<view class="table-value">
									<view class="num">
										{{item.projectLeader ? item.projectLeader : ''}}
									</view>
								</view>
							</view>
							</view>
						</view>
					</view>
				</view>
				<view class="item-value">
					<view class="table-title" v-if="item.projectNo">
						项目编号：
					</view>
					<view class="table-value">
						<view class="num">
							{{item.projectNo ? item.projectNo : "&nbsp;"}}
						</view>
					</view>
				</view>
				<view class="item-value">
					<view class="table-title">
						期望到货时间：
					</view>
					<view class="table-value">
						<view class="num" style="color:red;">
							2022-04-11
						</view>
					</view>
				</view>
				<view class="button" @click="batchTap">
					查看批次
				</view>
				<view class="plan-button" @click="supplierOpen(item)">
					供应商计划
				</view>
			</view>
		</view>
		<view class="detail-box-item">
			<view class="title">
				 物料收货人信息
			</view>
			<view class="info-item">
				<view class="left-box">
					<image class="user-icon" src="/static/image/people_icon.png" mode="aspectFit"></image>
					<view class="info-message">
						<view class="name">
							杨雪婷 133333333333
						</view>
						<view class="address">
							施工方哈迪斯规划局卡话感觉卡萨户籍卡
						</view>
					</view>
					
				</view>
				<view class="right-box">
					<image class="phone-icon" v-if="addressinfo.contacts[addressinfo.levelCurrent].phone && addressinfo.contacts[addressinfo.levelCurrent].phone != ''" src="/static/image/order/phone_icon.png" mode="aspectFit"
						@click="phoneCall(addressinfo.contacts[addressinfo.levelCurrent].phone)"></image>
					<image class="phone-icon" src="/static/image/order/edit_icon.png" mode="aspectFit"
						v-if="detailinfo.status == '1'" @click="editAddress"></image>
				</view>
			</view>
		</view>
		<view class="cell-item" @click="jumpHistory('collect')">
			<view class="left-box">
				收货记录
			</view>
			<view class="right-box">
				查看
			</view>
		</view>
	<!-- 	<view class="cell-item history" @click="jumpHistory('1')"
			v-if="detailinfo.status == '4' || detailinfo.status == '3'">
			<view class="left-box">
				退货记录
			</view>
			<view class="right-box">
				查看
			</view>
		</view> -->
		<view class="clearbottom"></view>
		<view class="button-box">
			<view class="button reject" @click="rejectTap">
				申请退货
			</view>
			<view class="button agree" @click="agreeTap">
				完成收货
			</view>
			<view class="button agree" @click="agreeTap">
				签收物料
			</view>
		</view>
		<view class="button-box" v-if="detailinfo.status == '1'">
			<view class="button reject" @click="closebill">
				关闭采购
			</view>
			<view class="button agree" @click="purchasesend">
				发送采购需求
			</view>
		</view>
		<view class="button-box" v-if="detailinfo.status == '2'">
			<view class="button reject" @click="closebill">
				关闭采购
			</view>
		</view>
		<view class="reject-reason-box" v-if="detailinfo.backAdvice">
			<view class="left-box">
				退回原因
			</view>
			<view class="right-box">
				{{detailinfo.backAdvice}}
			</view>
		</view>
		<remark v-if="remarkType" title="退回原因" @cancel="rejectCancel"></remark>
		<batch :info="item" v-if="batchType" @close="batchClose"></batch>
		<material :info="item" v-if="materialType" @close="materialClose"></material>
		<supplier :materialId="supplierInfo.materialId" :purchaseId="supplierInfo.purchaseId" v-if="supplierType" @close="supplierClose"></supplier>
	</view>
	</view>
</template>

<script>
	import * as api from '@/utils/api.js';
	import {getUrlJson} from '@/utils/utils.js';
	import supplier from '@/subcontractList/order/purchase/component/plan/supplier';
	import remark from '@/pages/mask/component/remark/remark';
	import material from '@/subcontractList/order/purchase/component/dialog/material';
	import batch from '@/subcontractList/order/purchase/component/dialog/batch';
	export default {
		components: {
			remark,
			batch,
			material,
			supplier
		},
		data() {
			return {
				id: null, // 详情id
				item: {}, // 选中物料信息
				detailinfo: {}, // 采购订单详情信息
				selectedAddress: {}, // 物料收货人信息
				addressinfo: {}, // 物料收货人信息列表
				supplierInfo:{},// 供应商计划传参信息
				supplierType:false, // 供应商计划显示状态
				remarkType: false, // 拒绝原因弹窗显示状态
				batchType: false, // 批次弹窗显示状态
				materialType: false, // 物料信息弹窗显示状态
				gradeList: ['', '#DA2B55', '#F93C33', '#FE940A', '#F6B45F', '#6D7278'], // 等级颜色列表
				statusList: [ // 订单状态列表
					{
						name: '待审核',
						color: '#fb3836'
					},
					{
						name: '通过',
						color: '#64c05b'
					},
					{
						name: '未通过',
						color: '#fb3836'
					},
					{
						name: '待接单',
						color: '#fb3836'
					},
					{
						name: '采购中',
						color: '#fb882b'
					},
					{
						name: '被退回',
						color: '#fb882b'
					},
					{},
					{},
					{},
					{},
					{},
					{
						name: '已关闭',
						color: '#adacac'
					},
				],
			}
		},
		onLoad(option) {
			if (option.id) {
				this.id = option.id;
				this.init();
				this.initcompanyDetail();
			}
		},
		methods: {
			jumpHistory:function(name){
				uni.navigateTo({
					url: '/subcontractList/order/purchase/waitbill/express/index/index?type='+ name
				});
			},
			/**
			 * 拨打电话号码
			 */
			phoneCall: function(data) {
				uni.makePhoneCall({
					phoneNumber: data //仅为示例
				});
			},
			/**
			 * 初始化详情
			 */
			init: function() {
				api.purchaseDetail({
					id: this.id
				}).then(res => {
					res.data.priority = res.data.priority.substring(0, 1);
					let str = '';
					res.data.purchaseMaterialVOS.forEach(result => {
						result.paramArr = [];
						str = '';
						result.param = JSON.parse(result.param);
						for (let i in result.param) {
							result.paramArr.push({
								'unit': i,
								'value': result.param[i]
							});
							str = str + i + '：' + result.param[i] + '/';
						}
						str = str.substring(0, str.length - 1);
						result.paramstr = str;
					})
					this.detailinfo = res.data;
				})
			},
			/**
			 * 初始化物料收货人信息
			 */
			initcompanyDetail: function() {
				let userinfo = getApp().globalData.userInfo;
				let data = {};
				userinfo.employee.forEach(res => {
					if (res.isDefault == "0") {
						data = res;
					}
				})
				api.companyDetail({
					id: data.tenantId
				}).then(res => {
					res.data.contacts.forEach(result => {
						if (result.flag == "0" && result.type == "2") {
							getApp().globalData.selected = result;
							this.selectedAddress = result;
						}
					})
					// getApp().globalData.selected = res.data.contacts[res.data.isApplet];
					console.log(getApp().globalData.selected);
					this.addressinfo = res.data;
				})
			},
			/**
			 * 前往收货记录/退货记录
			 */
			jumpHistory: function(name) {
				uni.navigateTo({
					url: '/subcontractList/order/purchase/waitbill/express/index/index?type=' + name + '&id=' + this.id
				});
			},
			
			/**
			 * 申请退货触发事件
			 */
			returnbill:function(){
				uni.showToast({
					title: '请到web进行操作',
					icon: 'none',
					duration: 2000
				});
				// uni.navigateTo({
				// 	url: '/subcontractList/order/purchase/waitbill/return/return_goods'
				// });
			},
			/**
			 * 关闭采购订单触发事件
			 */
			closebill: function() {
				let that = this;
				uni.showModal({
					title: '提示',
					content: '确认关闭采购？',
					success: function(res) {
						if (res.confirm) {
							api.purchaseclose({
								"id": that.id
							}).then(result => {
								uni.showToast({
									title: '关闭订单成功',
									mask: true,
									duration: 2000,
									complete() {
										setTimeout(function() {
											uni.navigateBack();
										}, 2000);
									}
								});
							})
						} else if (res.cancel) {}
					}
				});
			},
			/**
			 * 发送采购需求触发事件
			 */
			purchasesend: function() {
				let addressinfo = this.addressinfo;
				addressinfo = addressinfo.contacts[addressinfo.isApplet];
				let data = {
					"id": this.id,
					"receiveReq": {}
				}
				data.receiveReq.mobile = addressinfo.phone;
				data.receiveReq.receiveAddress = addressinfo.city;
				data.receiveReq.receiveUserName = addressinfo.name;
				api.purchasesend(data).then(res => {
					uni.showToast({
						title: '发送订单成功',
						mask: true,
						duration: 2000,
						complete() {
							setTimeout(function() {
								uni.navigateBack();
							}, 2000);
						}
					});
				})
			},

			/**
			 * 完成收货触发事件
			 */
			purchaseisFinish: function() {
				let that = this;
				uni.showModal({
					title: '提示',
					content: '确认完成收货？',
					success: function(res) {
						if (res.confirm) {
							let id = that.id;
							api.purchaseclose({"id": id}).then(results=>{
								api.purchaseisFinish({
									"id":id
								}).then(result => {
									uni.showToast({
										title: '完成订单成功',
										mask: true,
										duration: 2000,
										complete() {
											getApp().globalData.refreshPage = 'bill';
											setTimeout(function() {
												uni.navigateBack();
											}, 2000)
										}
									});
								})
								
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});

			},
			/**
			 * 物料信息悬浮窗打开触发事件
			 */
			materialOpen: function(item) {
				this.item = item;
				this.materialType = true;
			},
			materialClose:function(){
				this.materialType = false;
			},
			/**
			 * 拒绝弹窗开启触发事件
			 */
			rejectTap: function() {
				this.remarkType = true;
			},
			/**
			 * 拒绝弹窗关闭触发事件
			 */
			rejectCancel: function() {
				this.remarkType = false;
			},
			/**
			 * 供应商计划开启触发事件
			 */
			supplierOpen:function(item){
				console.log(item);
				this.supplierInfo = {materialId:item.materialId,purchaseId:this.id};
				this.supplierType = true;
			},
			/**
			 * 供应商计划关闭触发事件
			 */
			supplierClose:function(){
				this.supplierType = false;
			},
			/**
			 * 完成收货触发事件
			 */
			async agreeTap() {
				let that = this;
				let id = that.id;
				let res = await uni.showModal({title: '提示',content: '是否确认完成收货？'});
				console.log(res);
				if (res[1].confirm) {
					let result = await api.purchaseisFinish({id:id});
					if(result.data){
							that.finishBill();
					}else{
						res = await uni.showModal({title: '提示',content: '商品没有全部签收，是否完成？'});
						if (res[1].confirm) {
							that.finishBill();
						}
					}
				}
			},
			/**
			 * 完成订单接口
			 */
			finishBill:function(){
				api.purchaseclose({id:this.id}).then(res=>{
					uni.showToast({
						title: '完成收货成功',
						mask:true,
						duration: 2000,
						complete() {
							getApp().globalData.refreshPage = 'bill';
							setTimeout(function(){
								uni.navigateBack();
							},2000)
						}
					});
				});
			},
			batchTap:function(){
				this.batchType = true;
			},
			batchClose:function(){
				this.batchType = false;
			},
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		background-color: #eaeaea;
		min-height: 100vh;
	}

	.detail-box-item {
		box-sizing: border-box;

		.title {
			font-size: 32rpx;
			font-weight: bold;
			padding: 20rpx;
			padding-left: 20rpx;
			margin-left: 20rpx;
			position: relative;
			line-height: 60rpx;
		}

		.title::after {
			content: '';
			width: 6rpx;
			top: 35%;
			height: 30%;
			position: absolute;
			left: 0;
			background-color: #29ac7a;
		}

		.content-box {
			background-color: white;
			padding: 0 20rpx;

			.cell-item {
				display: flex;
				justify-content: space-between;
				line-height: 60rpx;
				font-size: 28rpx;

				.left-box {
					color: #818181;
				}
			}
		}

		.card-item {
			width: calc(100% - 40rpx);
			margin: 0 auto;
			box-sizing: border-box;
			background-color: white;
			padding: 20rpx;
			position: relative;
			.product-item {
				display: flex;

				.img-box {
					width: 160rpx;
					height: 160rpx;
					background-color: #eeeeee;
					overflow: hidden;

					.img {
						width: 50%;
						height: 50%;
						display: block;
						margin: 40rpx auto;
					}
				}

				.right-box {
					font-size: 26rpx;
					line-height: 40rpx;
					margin-left: 20rpx;

					.product-title {
						color: #ff5400;
						font-weight: bold;
					}

					.sign {
						color: #634ed1;
					}
				}
			}

			.table-box {
				display: table;
				width: 100%;

				.table-row {
					display: table-row;

					.table-item {
						// width: calc(100% / 3);

						display: table-cell;

					}
				}
			}

			.item-value {
				display: flex;
				font-size: 24rpx;
				line-height: 40rpx;

				.table-title {
					white-space: nowrap;
				}

				.table-value {
					display: flex;

					.num {
						color: #634ed1;
					}
				}
			}
			.button{
				position: absolute;
				bottom: 100rpx;
				right: 20rpx;
				width: fit-content;
				padding: 0 25rpx;
				height:70rpx;
				line-height: 70rpx;
				border-radius: 10rpx;
				color: white;
				font-size: 26rpx;
				text-align: center;
				background-color: #35af86;
			}
			.plan-button{
				margin-top:20rpx;
				height:60rpx;
				line-height:60rpx;
				color: #2396eb;
				text-align: center;
				font-size: 28rpx;
				background: #f0f5fa;
			}
		}
		.info-item{
			display: flex;
			justify-content: space-between;
			background-color: white;
			padding:20rpx;
			.left-box{
				display: flex;
				.user-icon{
					width: 70rpx;
					height: 70rpx;
				}
				.info-message{
					font-size: 26rpx;
					color: #888888;
					margin-left:20rpx;
				}
			}
			.right-box{
				.phone-icon{
					width: 70rpx;
					height: 70rpx;
				}
			}
		}
	}
	.cell-item{
		display: flex;
		justify-content: space-between;
		padding: 0 20rpx;
		background-color: white;
		line-height: 70rpx;
		height: 70rpx;
		box-sizing: border-box;
		font-size:28rpx;
		color: #888888;
		margin-top: 20rpx;
	}
	.clearbottom{
		width: 100%;
		height: calc(constant(safe-area-inset-bottom) + 120rpx);
		height: calc(env(safe-area-inset-bottom) + 120rpx);
	}
	.button-box{
		display: flex;
		width: calc(100% - 40rpx);
		position: fixed;
		left:0;
		bottom:0;
		padding:20rpx;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 20rpx);
		padding-bottom: calc(env(safe-area-inset-bottom) + 20rpx);
		background-color: #eaeaea;
		.button{
			color: white;
			border-radius: 10rpx;
			line-height: 70rpx;
			height: 70rpx;
			width: calc(100% - 10rpx);
			text-align: center;
			margin-right: 20rpx;
		}
		.reject{
			background-color: #afafaf;
		}
		.agree{
			background-color: #4169e2;
		}
		.button:last-child{
			margin-right: 0;
		}
	}
	.reject-reason-box{
		display: flex;
		position: fixed;
		width: 100%;
		left:0rpx;
		padding: 0 20rpx;
		box-sizing: border-box;
		bottom: calc(constant(safe-area-inset-bottom));
		bottom: calc(env(safe-area-inset-bottom));
		background-color: white;
		justify-content: space-between;
		line-height:80rpx;
		font-size: 28rpx;
	}
</style>
